﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.DataObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class GrantDO : EntityBase
    {
        /// <summary>
        /// 
        /// </summary>
        public GrantDO()
            : base(EntityType.Grants, "ProjectID", "GrantNumber")
        {
        }

        #region props
        private int _ProjectID;
        /// <summary></summary>
        public int ProjectID
        {
            get
            {
                return this._ProjectID;
            }
            set
            {
                this._ProjectID = value;
            }
        }

        private string _GrantNumber;
        /// <summary></summary>
        public string GrantNumber
        {
            get
            {
                return this._GrantNumber;
            }
            set
            {
                this._GrantNumber = value;
            }
        }

        private int _SerialNum;
        /// <summary></summary>
        public int SerialNum
        {
            get
            {
                return this._SerialNum;
            }
            set
            {
                this._SerialNum = value;
            }
        }

        private int _AccessionNum;
        /// <summary></summary>
        public int AccessionNum
        {
            get
            {
                return this._AccessionNum;
            }
            set
            {
                this._AccessionNum = value;
            }
        }

        private string _ActivityCode;
        /// <summary></summary>
        public string ActivityCode
        {
            get
            {
                return this._ActivityCode;
            }
            set
            {
                this._ActivityCode = value;
            }
        }

        private string _PhsAdminOrg;
        /// <summary></summary>
        public string PhsAdminOrg
        {
            get
            {
                return this._PhsAdminOrg;
            }
            set
            {
                this._PhsAdminOrg = value;
            }
        }

        private int _ApplTypeCode;
        /// <summary></summary>
        public int ApplTypeCode
        {
            get
            {
                return this._ApplTypeCode;
            }
            set
            {
                this._ApplTypeCode = value;
            }
        }

        private string _Title;
        /// <summary></summary>
        public string Title
        {
            get
            {
                return this._Title;
            }
            set
            {
                this._Title = value;
            }
        }

        private string _Abstract;
        /// <summary></summary>
        public string Abstract
        {
            get
            {
                return this._Abstract;
            }
            set
            {
                this._Abstract = value;
            }
        }

        private int _SupportYear;
        /// <summary></summary>
        public int SupportYear
        {
            get
            {
                return this._SupportYear;
            }
            set
            {
                this._SupportYear = value;
            }
        }
        #endregion

        #region Overrides of EntityBase

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="entityNode"></param>
        public override void Save(ref XmlDocument xDoc, XmlNode entityNode)
        {
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Score", this.Score.ToString());
            XmlDataUtil.UpdateAttribute(
                ref xDoc, entityNode, "Flag", string.IsNullOrEmpty(this.Flag) ? "" : this.Flag.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "ProjectID", this.ProjectID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "GrantNumber", this.GrantNumber);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Abstract", this.Abstract);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "AccessionNum", this.AccessionNum.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "ActivityCode", this.ActivityCode);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "ApplTypeCode", this.ApplTypeCode.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "PhsAdminOrg", this.PhsAdminOrg);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "SerialNum", this.SerialNum.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Title", this.Title);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "SupportYear",this.SupportYear.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "CollapsedID",this.CollapsedID.ToString());
        }
        #endregion

        public static List<EntityBase> ReadGrants(XmlDocument xDoc)
        {
            List<EntityBase> grants=new List<EntityBase>();
            XmlNodeList grantNodes = xDoc.DocumentElement.SelectNodes("//Grant");
            if(grantNodes !=null && grantNodes.Count>0)
            {
                foreach(XmlNode grantNode in grantNodes)
                {
                    GrantDO grant = GrantDO.Read(grantNode);
                    grants.Add(grant);
                }
            }
            return grants;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="grantNode"></param>
        /// <returns></returns>
        public static GrantDO Read(XmlNode grantNode)
        {
            GrantDO grant= new GrantDO();
            grant.Score = double.Parse(XmlDataUtil.GetAttributeValue(grantNode, "Score", "0"));
            grant.Flag = XmlDataUtil.GetAttributeValue(grantNode, "Flag", "");
            grant.ProjectID = int.Parse(XmlDataUtil.GetAttributeValue(grantNode, "ProjectID", "0"));
            grant.GrantNumber = XmlDataUtil.GetAttributeValue(grantNode, "GrantNumber", "");
            grant.Abstract = XmlDataUtil.GetAttributeValue(grantNode, "Abstract", "");
            grant.AccessionNum = int.Parse(XmlDataUtil.GetAttributeValue(grantNode, "AccessionNum", ""));
            grant.ActivityCode = XmlDataUtil.GetAttributeValue(grantNode, "ActivityCode", "");
            grant.ApplTypeCode = int.Parse(XmlDataUtil.GetAttributeValue(grantNode, "ApplTypeCode", "1"));
            grant.PhsAdminOrg = XmlDataUtil.GetAttributeValue(grantNode, "PhsAdminOrg", "");
            grant.SerialNum = int.Parse(XmlDataUtil.GetAttributeValue(grantNode, "SerialNum", "0"));
            grant.Title = XmlDataUtil.GetAttributeValue(grantNode, "Title", "");
            grant.SupportYear = int.Parse(XmlDataUtil.GetAttributeValue(grantNode, "SupportYear", "0"));
            grant.CollapsedID = int.Parse(XmlDataUtil.GetAttributeValue(grantNode, "CollapsedID", "0"));

            return grant;
        }
    }
}
